package com.company.aicrawlers.controller.api;

import com.company.aicrawlers.common.dto.ApiResponse;
import com.company.aicrawlers.common.dto.PageResponse;
import com.company.aicrawlers.entity.Product;
import com.company.aicrawlers.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 产品API控制器
 *
 * @author AI Crawlers Team
 * @date 2025-10-10
 */
@Slf4j
@RestController
@RequestMapping("/api/products")
public class ProductApiController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 分页查询产品列表
     * 
     * 接口：GET /api/products
     *
     * @param page 页码，默认1
     * @param size 每页大小，默认10
     * @param keyword 搜索关键词（产品名称、品牌）
     * @param category 产品类别
     * @param isOurProduct 是否我方产品（1:是 0:否）
     * @param status 状态
     * @return 分页数据
     */
    @GetMapping(value = "", name = "getProductList")
    public ApiResponse<PageResponse<Product>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer isOurProduct,
            @RequestParam(required = false) Integer status
    ) {
        log.info("查询产品列表，page: {}, size: {}, keyword: {}, category: {}, isOurProduct: {}, status: {}", 
                page, size, keyword, category, isOurProduct, status);
        
        PageResponse<Product> pageData = productService.getPageList(page, size, keyword, category, isOurProduct, status);
        return ApiResponse.success(pageData);
    }
    
    /**
     * 获取产品详情
     * 
     * 接口：GET /api/products/{id}
     *
     * @param id 产品ID
     * @return 产品对象
     */
    @GetMapping(value = "/{id}", name = "getProductById")
    public ApiResponse<Product> getById(@PathVariable("id") Long id) {
        log.info("获取产品详情，id: {}", id);
        
        Product product = productService.getById(id);
        return ApiResponse.success(product);
    }
    
    /**
     * 创建产品
     * 
     * 接口：POST /api/products
     *
     * @param product 产品对象
     * @return 产品ID
     */
    @PostMapping(value = "", name = "createProduct")
    public ApiResponse<Long> create(@RequestBody Product product) {
        log.info("创建产品，name: {}, brand: {}, isOurProduct: {}", 
                product.getProductName(), product.getBrand(), product.getIsOurProduct());
        
        Long productId = productService.create(product);
        return ApiResponse.success(productId, "创建成功");
    }
    
    /**
     * 更新产品
     * 
     * 接口：PUT /api/products/{id}
     *
     * @param id 产品ID
     * @param product 产品对象
     * @return 成功消息
     */
    @PutMapping(value = "/{id}", name = "updateProduct")
    public ApiResponse<Void> update(@PathVariable("id") Long id, @RequestBody Product product) {
        log.info("更新产品，id: {}", id);
        
        product.setProductId(id);
        productService.update(product);
        return ApiResponse.success("更新成功");
    }
    
    /**
     * 删除产品
     * 
     * 接口：DELETE /api/products/{id}
     *
     * @param id 产品ID
     * @return 成功消息
     */
    @DeleteMapping(value = "/{id}", name = "deleteProduct")
    public ApiResponse<Void> delete(@PathVariable("id") Long id) {
        log.info("删除产品，id: {}", id);
        
        productService.delete(id);
        return ApiResponse.success("删除成功");
    }
    
    /**
     * 批量删除产品
     * 
     * 接口：DELETE /api/products/batch
     *
     * @param ids 产品ID列表
     * @return 成功消息
     */
    @DeleteMapping(value = "/batch", name = "batchDeleteProducts")
    public ApiResponse<Void> batchDelete(@RequestBody List<Long> ids) {
        log.info("批量删除产品，ids: {}", ids);
        
        productService.batchDelete(ids);
        return ApiResponse.success("批量删除成功");
    }
    
    /**
     * 批量导入产品（Excel）
     * 
     * 接口：POST /api/products/import
     *
     * @param file Excel文件
     * @return 导入数量
     */
    @PostMapping(value = "/import", name = "importProducts")
    public ApiResponse<Integer> batchImport(@RequestParam("file") MultipartFile file) {
        log.info("批量导入产品，文件名: {}", file.getOriginalFilename());
        
        Integer count = productService.batchImport(file);
        return ApiResponse.success(count, "成功导入 " + count + " 条数据");
    }
    
    /**
     * 获取所有产品类别
     * 
     * 接口：GET /api/products/categories
     *
     * @return 类别列表
     */
    @GetMapping(value = "/categories", name = "getAllProductCategories")
    public ApiResponse<List<String>> getAllCategories() {
        log.info("获取所有产品类别");
        
        List<String> categories = productService.getAllCategories();
        return ApiResponse.success(categories);
    }
    
    /**
     * 获取所有品牌
     * 
     * 接口：GET /api/products/brands
     *
     * @return 品牌列表
     */
    @GetMapping(value = "/brands", name = "getAllBrands")
    public ApiResponse<List<String>> getAllBrands() {
        log.info("获取所有品牌");
        
        List<String> brands = productService.getAllBrands();
        return ApiResponse.success(brands);
    }
}

